package com.bawei.erp.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.bawei.common.StockException;
import com.bawei.erp.mapper.StockMapper;
import com.bawei.erp.model.InDetails;
import com.bawei.erp.model.OutDetails;
import com.bawei.erp.model.Stock;
import com.bawei.erp.model.StockExample;
import com.bawei.erp.service.StockInDetailsService;
import com.bawei.erp.service.StockOutDetailsService;
import com.bawei.erp.service.StockService;
import com.bawei.util.BeanUtil;

@Service
public class StockServiceImpl implements StockService {

	@Resource
	private StockMapper stockMapper;

	@Resource
	private StockInDetailsService stockInDetailsService;

	@Resource
	private StockOutDetailsService stockOutDetailsService;

	@Override
	public void addStock(Stock stock) {
		stockMapper.insert(stock);
	}

	@Override
	public void updateStock(Stock stock) {
		stockMapper.updateByPrimaryKeySelective(stock);
	}

	@Override
	public void deleteStock(Long id) {
		stockMapper.deleteByPrimaryKey(id);
	}

	@Override
	public List<Stock> getStock() {
		return stockMapper.selectByExample(null);
	}

	@Override
	public Stock getStockById(Long id) {
		return stockMapper.selectByPrimaryKey(id);
	}

	@Override
	public void updateStockBySKU(Stock stock) {
		Stock s = getStockBySKU(stock.getSku());
		BeanUtil.copyProperty(stock, s);
		stockMapper.updateByPrimaryKey(s);
	}

	@Override
	public void stockIn(String sku, int count, Byte type) {

		Stock st = getStockBySKU(sku);
		if (st.getIsLock() == 1)
			throw new StockException("库存商品已锁定，sku:" + sku);

		Stock stock = getStockBySKU(sku);
		stock.setStockCount(stock.getStockCount() + count);
		stock.setPurchaseCount(stock.getPurchaseCount() - count);
		stockMapper.updateByPrimaryKey(stock);

		// 新增入库明细
		InDetails in = new InDetails();
		in.setCount(count);
		in.setSku(sku);
		in.setInTime(new Date());
		in.setType(type);
		in.setInUser("");
		stockInDetailsService.addInDetails(in);

	}

	@Override
	public void stockOut(String sku, int count, Byte type) {

		Stock st = getStockBySKU(sku);
		if (st.getIsLock() == 1)
			throw new StockException("库存商品已锁定，sku:" + sku);

		Stock stock = getStockBySKU(sku);
		stock.setStockCount(stock.getStockCount() - count);
		stock.setLockCount(stock.getLockCount() - count);
		stockMapper.updateByPrimaryKey(stock);
		// 新增出库明细
		OutDetails out = new OutDetails();
		out.setCount(count);
		out.setSku(sku);
		out.setOutTime(new Date());
		out.setOutUser("");
		out.setType(type);
		stockOutDetailsService.addOutDetails(out);
	}

	@Override
	public Stock getStockBySKU(String sku) {
		StockExample example = new StockExample();
		example.createCriteria().andSkuEqualTo(sku);
		List<Stock> stocks = stockMapper.selectByExample(example);
		if (stocks == null || stocks.size() == 0)
			return null;
		return stocks.get(0);
	}

	@Override
	public int getValidCount(String sku) {
		Stock stock = getStockBySKU(sku);
		return stock.getStockCount() + stock.getPurchaseCount() - stock.getLockCount();
	}

	@Override
	public void lockStock(String sku, int count) {
		Stock stock = getStockBySKU(sku);
		stock.setLockCount(stock.getLockCount() + count);
		stockMapper.updateByPrimaryKey(stock);
	}

	@Override
	public void lock(String sku) {
		
		Stock stock = getStockBySKU(sku);
		stock.setIsLock((byte) 1);
		stockMapper.updateByPrimaryKeySelective(stock);
	}

	@Override
	public void unLock(String sku) {

		Stock stock = getStockBySKU(sku);
		stock.setIsLock((byte) 0);
		stockMapper.updateByPrimaryKeySelective(stock);
	}
}
